<template>
	<view class="content">
		<view class="password-box">
			<u-message-input @finish="finish" focus active-color="#FF4E47" dot-fill breathe :maxlength="6" mode="middleLine"></u-message-input>
		</view>

		<!-- <u-keyboard z-index="1" @backspace="backspace" confirm-text=" " @confirm="show = true" random :dot-enabled="false" @change="change"
		 :cancel-btn="false" :mask-close-able="false" safe-area-inset-bottom ref="uKeyboard" :mask="false" :tips="type"
		 v-model="show"></u-keyboard> -->

		<u-popup mode="center" :mask-close-able="false" closeable border-radius="20" v-model="smsPopup">
			<view class="popup">
				<view class="title">{{type}}需要验证您的身份</view>
				<view class="popup-tip">验证码{{time < 60 ? '已' : '将'}}发送至{{phone}}</view>
				<view class="code">
					<u-input :clearable="false" v-model="smsCode" placeholder="请输入验证码"></u-input>
					<u-button @click="getCode" size="mini" :custom-style="customSstyle" ripple>{{time == 60 ? '获取验证码' : time +'s后重新获取'}}</u-button>
				</view>
				<view class="determine" @click="submit">
					确定
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// show: true,
				smsPopup: false,
				smsCode: '',
				type: '',
				time: 60,
				customSstyle: {
					backgroundColor: "#F39A52",
					color: "#ffffff",
					border: 'none'
				},
				phone: ''
			};
		},
		onLoad() {
			// this.type = options.type == 1 ? '修改支付密码' : '设置支付密码'
			this.type = '设置支付密码'
			let reg = /^(\d{3})\d{4}(\d{4})$/,
				userInfo = uni.getStorageSync('userInfo')
			userInfo.userName = userInfo.userName.replace(reg, '$1****$2')
			this.phone = userInfo.userName
		},
		methods: {
			async getCode() {
				if (this.time != 60) return
				await this.$u.get('sms/send/identity/' + uni.getStorageSync('userInfo').userName)
				uni.showToast({
					title: '验证码已发送'
				})
				let play = () => {
					this.time--
					if (this.time <= 0) {
						this.time = 60
						clearInterval(this.timer)
					}
				}
				play()
				this.timer = setInterval(play, 1000)
			},
			submit() {
				if (this.smsCode == '') {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					return
				}
				this.$u.post('userpaypassword/updatePassword', {
					couponPassword: this.value,
					smsCode: this.smsCode
				}).then(res => {
					uni.showToast({
						title: '操作成功'
					});
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				}).catch(error => {
					this.smsPopup = false
					this.smsCode = ''
					this.value = ''
				})
			},
			finish(e) {
				var reg = new RegExp(/^(?![^a-zA-Z]+$)(?!\D+$)/);
				if (reg.test(e)) {
					this.smsPopup = true
					this.value = e
				} else {
					uni.showToast({
						title: '密码必须包含数字和字母',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.popup {
		padding: 30rpx 50rpx;

		.determine {
			line-height: 80rpx;
			color: #FFFFFF;
			text-align: center;
			margin-top: 100rpx;
			background: linear-gradient(90deg, #FF3868, #FC6161);
			border-radius: 10rpx;
		}

		.code {
			padding-top: 50rpx;
			display: flex;
			align-items: center;
			border-bottom: 1rpx solid #E1E0E0;
		}

		.title {
			padding-top: 40rpx;
			font-size: 32rpx;
			text-align: center;
		}

		.popup-tip {
			font-size: 28rpx;
			margin: 30rpx 0;
			text-align: center;
			color: #AAAAAA;
		}
	}

	.password-box {
		height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	page {
		background-color: #f7f7f7;
	}
</style>
